<template>
  <template v-for="item in props.menuData">
    <el-menu-item v-if="!item.children || item.children.length == 0" :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`" @click="handleChilck(item, `${props.index}-${item.meta.id}`)">
      <el-icon size="20">
        <!-- 动态渲染icon -->
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon size="20">
          <!-- 动态渲染icon -->
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <!-- 递归复用组件自身 -->
      <three-menu :index="`${props.index}-${item.meta.id}`" :menuData="item.children" />
    </el-sub-menu>
  </template>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { CounterStore } from '../store/index'
// 传入router的ts
import type { MenuType } from '../router/routerType'
// 获取父组件自定义数据的传值
const props = defineProps(['menuData', 'index'])

// 创建router实例
const router = useRouter()
// 改变pinia数据
const store = CounterStore()
const addMenu = (state: MenuType) => {
  // 对数据进行去重
  if (store.state.selectMenu.findIndex((item: { path: string; }) => item.path === state.path) === -1) {
    store.state.selectMenu.push(state)
  }
}
// 修改pinia中的值
const updataMenuAction = (active:string)=>{
  store.state.menuActive = active
}
// 点击菜单
const handleChilck = (item: { meta: MenuType }, active: string) => {
  router.push(item.meta.path)
  addMenu(item.meta)
  updataMenuAction(active)
}
</script>

<style scoped></style>